﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Defector
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Submit : Window
    {

        public int reportNumber;
        public SqlConnection con;
        SqlDataAdapter adapter = new SqlDataAdapter();

        public Submit()
        {
            InitializeComponent();
        }

        public void start()
        {
            loadSoftwareData();
        }
        
        //Load the software data into the software combo box
        public void loadSoftwareData()
        {


            DataSet ds = new DataSet();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT * FROM Softwares";
                adapter.SelectCommand = cmd;
                adapter.Fill(ds, "Software");

                DataRow nRow = ds.Tables["Software"].NewRow();

                //Binding the data to the combobox.
                softwareComboBox.DataContext = ds.Tables["Software"].DefaultView;

                //To display category name
                softwareComboBox.DisplayMemberPath =
                   ds.Tables["Software"].Columns["Software_Name"].ToString();
                //To store the ID as hidden
                softwareComboBox.SelectedValuePath =
                   ds.Tables["Software"].Columns["Software_Id"].ToString();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while loading software data.");
            }
        }

        private void submitDefectButton_Click(object sender, RoutedEventArgs e)
        {
            //Check if form is completely filled out
            if (headlineInput.Text != "" && descriptionInput.Text != "" && priorityInput.SelectedIndex > 0 && softwareComboBox.SelectedIndex > -1)
            {

                string sqlIns = "INSERT INTO Trouble_Report (Date_Submitted, PersonId, Headline, Description, SoftwareSoftware_Id, Priority) VALUES (@Date_Submitted, @PersonId, @Headline, @Description, @SoftwareSoftware_Id, @Priority)";

                try
                {
                    con.Open();
                    SqlCommand cmdIns = new SqlCommand(sqlIns, con);
                    cmdIns.Parameters.AddWithValue("@Date_Submitted", DateTime.Now);
                    cmdIns.Parameters.AddWithValue("@PersonId", 1);
                    cmdIns.Parameters.AddWithValue("@Headline", headlineInput.Text);
                    cmdIns.Parameters.AddWithValue("@Description", descriptionInput.Text);
                    cmdIns.Parameters.AddWithValue("@SoftwareSoftware_Id", softwareComboBox.SelectedValue);
                    cmdIns.Parameters.AddWithValue("@Priority", priorityInput.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));
                    cmdIns.ExecuteNonQuery();
                    cmdIns.Dispose();
                    cmdIns = null;
                    con.Close();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred while submitting the defect.");
                }
            }
            else
                MessageBox.Show("Please complete the form!");
        }
    }
}
